<?php
class SwfuploadWidget extends CWidget  {
	
	// Upload backend settings
    public $upload_url;
    public $preserve_relative_urls;//false
    public $file_post_name;//Filedata
    public $post_params;//{}
    public $use_query_string;//false
    public $requeue_on_error;//false
    public $http_success;//[]
    public $assume_success_timeout;//0
    
    // File Settings
    public $file_types;//"*.*"
    public $file_types_description;//"All Files"
    public $file_size_limit;//0 Default zero means "unlimited"
    public $file_upload_limit;//0
    public $file_queue_limit;//0
    
    // Flash Settings
    public $flash_url;//swfupload.swf
    public $flash9_url;//swfupload_fp9.swf
    public $prevent_swf_caching;//true
    
    // Button Settings
    public $button_image_url;//""
    public $button_width=180;//1
    public $button_height=18;//1
    public $button_text='<span class="button">选择文件</span>';//""
    public $button_text_style='.button { font-family: Helvetica, Arial, sans-serif; font-size: 12pt; }';//color: #000000; font-size: 16pt;
    public $button_text_top_padding=0;//0
    public $button_text_left_padding=18;//0
    public $button_action='js:SWFUpload.BUTTON_ACTION.SELECT_FILE';//SWFUpload.BUTTON_ACTION.SELECT_FILES
    public $button_disabled;//false
    public $button_placeholder_id;//""
    public $button_placeholder;//null
    public $button_cursor='js:SWFUpload.CURSOR.HAND';//SWFUpload.CURSOR.ARROW
    public $button_window_mode='js:SWFUpload.WINDOW_MODE.TRANSPARENT';//SWFUpload.WINDOW_MODE.WINDOW
    
    // Debug Settings
    public $debug;//false
    
    // Event Handlers
    public $swfupload_preload_handler='js:preLoad';//null
    public $swfupload_load_failed_handler='js:loadFailed';//null
    public $swfupload_loaded_handler;//null
    public $file_dialog_start_handler='js:fileDialogStart';//null
    public $file_queued_handler='js:fileQueued';//null
    public $file_queue_error_handler='js:fileQueueError';//null
    public $file_dialog_complete_handler='js:fileDialogComplete';//null
    
    public $upload_resize_start_handler;//null
    public $upload_start_handler='js:uploadStart';//null
    public $upload_progress_handler='js:uploadProgress';//null
    public $upload_error_handler='js:uploadError';//null
    public $upload_success_handler='js:uploadSuccess';//null
    public $upload_complete_handler='js:uploadComplete';//null
    
    public $mouse_click_handler;//null
    public $mouse_out_handler;//null
    public $mouse_over_handler;//null
    
    public $debug_handler;//this.debugMessage
    
    public $custom_settings;//{}

    public $options=array();

    public function init() {
    	if ($this->button_placeholder_id===null) {
    		$this->button_placeholder_id='spanButtonPlaceholder';
    		echo "<span class=\"wrapUploadButton\"><span id=\"{$this->button_placeholder_id}\"></span></span>";
    	}
    	if ($this->custom_settings===null) {
    		$this->custom_settings=array(
    			'progressTarget'=>'divFileProgressContainer',
    		);
    		echo "<div id=\"{$this->custom_settings['progressTarget']}\"></div>";
    	}
		$this->registerClientScript();
    }

    public function registerClientScript() {
    	$id=$this->getId();

    	$baseUrl = Yii::app()->getAssetManager()->publish(dirname(__FILE__).'/assets',false,-1,YII_DEBUG);

    	if ($this->flash_url===null) {
    		$this->flash_url=$baseUrl.'/swfupload.swf';
    	}
    	if ($this->flash9_url===null) {
    		$this->flash9_url=$baseUrl.'/swfupload_fp9.swf';
    	}
    	if ($this->button_image_url===null) {
    		$this->button_image_url=$baseUrl.'/SmallSpyGlassWithTransperancy_17x18.png';
    	}
    	if ($this->file_types && $this->file_types_description===null) {
    		$this->file_types_description='支持的文件类型';
    	}

    	$cs = Yii::app()->getClientScript();

        $cs->registerScriptFile($baseUrl.'/swfobject.js');
        $cs->registerScriptFile($baseUrl.'/swfupload.noswfobject.js');
        $cs->registerScriptFile($baseUrl.'/swfupload.queue.js');
        $cs->registerScriptFile($baseUrl.'/fileprogress.js');
        $cs->registerScriptFile($baseUrl.'/handlers.js');
        $cs->registerCssFile($baseUrl.'/default.css');

        $acOptions=$this->getClientOptions();
		$options=$acOptions===array()?'{}' : CJavaScript::encode($acOptions);
        $cs->registerScript('Yii.'.__CLASS__.'#'.$id.'DefineVar', "var swfu_{$id};",CClientScript::POS_HEAD);
        $cs->registerScript('Yii.'.__CLASS__.'#'.$id, "swfu_{$id}=new SWFUpload({$options});", CClientScript::POS_LOAD);
    }

    protected function getClientOptions()
	{
		static $properties=array(
			'upload_url','preserve_relative_urls','file_post_name','post_params','use_query_string',
			'requeue_on_error', 'http_success', 'assume_success_timeout', 
			'file_types','file_types_description','file_size_limit','file_upload_limit','file_queue_limit',
			'flash_url', 'flash9_url', 'prevent_swf_caching', 
			'button_image_url','button_width','button_height', 'button_text', 'button_text_style', 'button_text_top_padding','button_text_left_padding','button_disabled','button_placeholder_id','button_placeholder', 
			'debug', 'custom_settings',
			'button_action','button_cursor','button_window_mode', 
			'swfupload_preload_handler', 'swfupload_load_failed_handler','swfupload_loaded_handler',
			'file_dialog_start_handler','file_queued_handler','file_queue_error_handler','file_dialog_complete_handler',
			'upload_resize_start_handler','upload_start_handler','upload_progress_handler','upload_error_handler','upload_success_handler','upload_complete_handler',
			'mouse_click_handler','mouse_out_handler','mouse_over_handler','debug_handler');
			
		static $functions=array(
			'button_action','button_cursor','button_window_mode', 
			'swfupload_preload_handler', 'swfupload_load_failed_handler','swfupload_loaded_handler',
			'file_dialog_start_handler','file_queued_handler','file_queue_error_handler','file_dialog_complete_handler',
			'upload_resize_start_handler','upload_start_handler','upload_progress_handler','upload_error_handler','upload_success_handler','upload_complete_handler',
			'mouse_click_handler','mouse_out_handler','mouse_over_handler','debug_handler');

		$options=$this->options;
		foreach($properties as $property)
		{
			if($this->$property!==null)
				$options[$property]=$this->$property;
		}
		foreach($functions as $func)
		{
			if(is_string($this->$func) && strncmp($this->$func,'js:',3))
				$options[$func]='js:'.$this->$func;
		}

		return $options;
	}
}
